﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer.Admin.Copy.Aggregation;
using System.Transactions;
using DataAccessLayer.Properties;
using IMModel.Domain;

namespace DataAccessLayer
{
    public partial class SqlDataProvider : IDataProvider
    {
        public void CopyIndexMarkStruct2(int fromIDTerm, int toIDTerm)
        {
            ClearIndexMarkStruct(toIDTerm);
            CopyIndexMark c = new CopyIndexMark(CharacteristicCopyMode.WithGradations);
            c.StartCopyAll(fromIDTerm, toIDTerm);
        }

        public void CopyUniversityStruct2(int fromIDTerm, int toIDTerm)
        {
            ClearUniversityStruct(toIDTerm);
            CopyUniversity c = new CopyUniversity();
            c.StartCopyAll(fromIDTerm, toIDTerm);
        }

        public void CopyPersonDegrees(int fromIDTerm, int toIDTerm)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    foreach (PersonDegree pd in db.PersonDegrees.Where(w => w.IDTerm == fromIDTerm))
                    {
                        PersonDegree new_pd = new PersonDegree
                        {
                            IDDegree = pd.IDDegree,
                            IDPerson = pd.IDPerson,
                            IDTerm = toIDTerm,
                        };
                        db.PersonDegrees.InsertOnSubmit(new_pd);
                    }

                    db.SubmitChanges();
                    transaction.Complete();
                }
            }
        }

        public void CopyAdmCategoriesByTerm(int fromIDTerm, int toIDTerm)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    foreach (CategoryTerm ct in db.CategoryTerms.Where(w => w.IDTerm == fromIDTerm))
                    {
                        CategoryTerm new_ct = new CategoryTerm
                        {
                            IDCategory = ct.IDCategory,
                            IDTerm = toIDTerm,
                            Weight = ct.Weight
                        };
                        db.CategoryTerms.InsertOnSubmit(new_ct);
                    }

                    db.SubmitChanges();
                    transaction.Complete();
                }
            }
        }

        public void CopyIndexMarkElement(DTO.DTOBase copiedObject, int toIDTerm, DTO.DTOBase targetObject)
        {
            CopyIndexMark c = new CopyIndexMark(CharacteristicCopyMode.WithoutGradations);
            if (copiedObject is DTO.AdmSection)
            {
                int? IDParentSection = (targetObject is DTO.AdmSection) ? (int?)(targetObject as DTO.AdmSection).ID : null;
                DTO.AdmSection copiedSection = copiedObject as DTO.AdmSection;
                c.HierarchicalCopySection(copiedSection.IDSectionTerm, IDParentSection, copiedSection.IDTerm, toIDTerm);
            }
            if (copiedObject is DTO.AdmCharacteristic)
            {
                int? IDParentSection = (targetObject is DTO.AdmSection) ? (int?)(targetObject as DTO.AdmSection).ID : null;
                DTO.AdmCharacteristic copiedCharacteristic = copiedObject as DTO.AdmCharacteristic;
                c.CopyCharacteristic(copiedCharacteristic.IDCharacteristicTerm, IDParentSection, copiedCharacteristic.IDTerm, toIDTerm);
            }
        }

        public void CopyUniversityElement(DTO.DTOBase copiedObject, int toIDTerm, DTO.DTOBase targetObject)
        {
            CopyUniversity c = new CopyUniversity();
            if (copiedObject is DTO.AdmDepartment)
            {
                int? IDParentDepartment = (targetObject is DTO.AdmDepartment) ? (int?)(targetObject as DTO.AdmDepartment).IDDepartment : null;
                DTO.AdmDepartment copiedDepartment = copiedObject as DTO.AdmDepartment;
                c.HierarchicalCopyDepartment((int)copiedDepartment.IDDepartment, IDParentDepartment, copiedDepartment.IDTerm, toIDTerm);
            }
            if (copiedObject is DTO.AdmRespondent)
            {
                int? IDParentDepartment = (targetObject is DTO.AdmDepartment) ? (int?)(targetObject as DTO.AdmDepartment).IDDepartment : null;
                DTO.AdmRespondent copiedRespondent = copiedObject as DTO.AdmRespondent;
                c.CopyRespondent(copiedRespondent.IDRespondent, IDParentDepartment, copiedRespondent.IDTerm, toIDTerm);
            }
        }

        public void CopyGradationStructElement(DTO.DTOBase copiedObject, int toIDTerm, DTO.DTOBase targetObject)
        {
            CopyGradations c = new CopyGradations();
            if (copiedObject is DTO.AdmGradation)
            {
                int? IDParentRelation = (targetObject is DTO.AdmGradation) ? (int?)(targetObject as DTO.AdmGradation).IDGradationTerm : null;
                DTO.AdmGradation copiedGradation = copiedObject as DTO.AdmGradation;
                c.HierarchicalCopyGradation(copiedGradation.IDGradationTerm, IDParentRelation, copiedGradation.IDTerm, toIDTerm);
            }
        }
    }
}
